package com.bw.house.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.bw.house.client.HouseOrder;
import com.bw.house.common.ResponseResult;
import com.bw.house.common.auth.UserInfo;
import com.bw.house.common.idwork.IdWorker;
import com.bw.house.entity.TbHouse;
import com.bw.house.entity.TbOrder;
import com.bw.house.service.ITbOrderService;
import com.bw.house.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 邢晋宁
 * @since 2020-09-04
 */
@RestController
@RequestMapping("order")
public class TbOrderController {

    @Autowired
    private ITbOrderService orderService;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private HouseOrder houseOrder;

    @Autowired
    private IdWorker idWorker;


    /**
     * @Description:C端房源列表
     * @return: com.bw.house.common.ResponseResult
     **/
    @GetMapping("findById/{id}")
    public ResponseResult findById(@PathVariable("id") Long id, HttpServletRequest request){
        ResponseResult result=ResponseResult.SUCCESS();
        UserInfo loginUser = userUtil.getLoginUser(request);
        try {
            TbHouse house = houseOrder.findById(id);
            //house.setNum(0);

            TbOrder tbOrder = new TbOrder();
            tbOrder.setCid(id);
            tbOrder.setCode(idWorker.nextId()+"");
            tbOrder.setCreateTime(new Date());
            tbOrder.setPrice(house.getDprice());
            tbOrder.setCname(house.getName());
            tbOrder.setSuid(loginUser.getId());
            tbOrder.setSuname(loginUser.getUsername());
            tbOrder.setStatus(0);
            orderService.saveOrUpdate(tbOrder);
            result.setMessage("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("保存失败");
        }
        return result;
    }
    @GetMapping("orderList")
<<<<<<< HEAD
    public ResponseResult orderList( HttpServletRequest request){
=======
    public ResponseResult orderList(HttpServletRequest request){
>>>>>>> 9c94696385770f5f4f82ce1a529b6d48167ca2c5
        ResponseResult result=ResponseResult.SUCCESS();
        UserInfo loginUser = userUtil.getLoginUser(request);
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("suid",loginUser.getId());
            List list = orderService.list(queryWrapper);
            result.setResult(list);
            result.setMessage("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("查询失败");
        }
        return result;
    }

}
